package lab15notepad;

public class SimpleNotepad implements INotepad {

	private String title;
	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	private Page[] pages;
	private int pageNumber;
	private static final int DEFAULT_PAGE_NUMBER=32;
	private static final String DEFAULT_TITLE="Untitled";
	
	/**
	 * @return the defaultTitle
	 */
	public static String getDefaultTitle() {
		return DEFAULT_TITLE;
	}

	/**
	 * @return the pages
	 */
	protected Page[] getPages() {
		return pages;
	}
	
	protected Page getPage(int page) {
		return pages[page];
	}

	/**
	 * @param pages the pages to set
	 */
	protected void setPages(Page[] pages) {
		this.pages = pages;
	}
	
	protected void setPage(Page page, int i) {
		this.pages[i]=page;
	}

	/**
	 * @param pageNumber the pageNumber to set
	 */
	protected void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	/**
	 * @return the pageNumber
	 */
	protected int getPageNumber() {
		return pageNumber;
	}

	/**
	 * @return the defaultPageNumber
	 */
	protected static int getDefaultPageNumber() {
		return DEFAULT_PAGE_NUMBER;
	}

	public SimpleNotepad(String title, int pageNumber) {
		super();
		this.title=title;
		this.pageNumber = pageNumber;
		this.pages=new Page[pageNumber];
		for (int i = 0; i < pageNumber; i++) pages[i]=new Page();
	}
	
	public SimpleNotepad() {
		this(DEFAULT_TITLE, DEFAULT_PAGE_NUMBER);
	}
	

	protected boolean isPage(int page) {
		if (page>=pageNumber||page<0) return false;
		return true;
	}
	
	@Override
	public void addText(String text, int page) {
		if (isPage(page)) pages[page].append(text);
		else System.out.println("Illegal page number.");
	}

	@Override
	public void replaceText(String text, int page) {
		if (isPage(page)) pages[page].replace(text);
		else System.out.println("Illegal page number.");
	}

	@Override
	public void deleteText(int page) {
		if (isPage(page)) pages[page].delete();
		else System.out.println("Illegal page number.");
	}

	@Override
	public void showPage(int page) {
		if (!isPage(page)) System.out.println("Illegal page number.");
		else if (!pages[page].isEmpty())	pages[page].view();
		else System.out.println("Page is empty.");
	}

	@Override
	public void showAll() {
		for (Page p : pages) {
			if (!p.isEmpty()) p.view();
		}
	}

	@Override
	public void setTitle(String title, int page) {
		pages[page].setTitle(title);
		
	}

	@Override
	public void searchWord(String word) {
		for (Page p : pages) {
			if (p.searchWord(word)) {
				System.out.println("The word \"" + word + "\"" + " appears in the text of at least one page.");
				return;
			}
		}
		System.out.println("The word \"" + word + "\" does not appear in the text of any page.");
	}

	@Override
	public void printAllPagesWithDigits() {
		for (Page p : pages) {
			if (p.containsDigits()) p.view();
		}
	}
}